package cn.itcast.nems.student.controller.b;

import cn.itcast.nems.oldems.api.StudentContactApi;
import cn.itcast.nems.oldems.dto.StudentContactEditDto;
import cn.itcast.nems.oldems.dto.StudentContactListDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生联系方式
 */
@Slf4j
@RestController
@RequestMapping("/b/student/{studentId}/contact")
@RequiredArgsConstructor
public class StudentContactController {

    private final StudentContactApi studentContactApi;

    /**
     * 列出一个学生的所有联系方式
     * <p>
     * 与李勇与廉德明老师商议，联系方式由主要手机号+附表共同组成
     *
     * @param studentId
     * @return
     */
    @GetMapping()
    public ResponseEntity<List<StudentContactListDto>> listAll(
            @PathVariable("studentId") int studentId
    ) {
        return ResponseEntity.ok(
                studentContactApi.listStudentContactInfoByStudentId(studentId)
        );
    }

    /**
     * 新增、编辑、删除联系方式
     * <p>
     * 操作使用action区分
     * 编辑为EDIT， 新增为ADD，删除为DELETE
     *
     * @param bo
     * @return
     */
    @PutMapping()
    public ResponseEntity<Boolean> edit(@RequestBody StudentContactEditDto bo) {
        return switch (bo.getAction()) {
            case "ADD" -> ResponseEntity.ok(studentContactApi.addStudentContactInfo(bo));
            case "EDIT" -> ResponseEntity.ok(studentContactApi.editStudentContactInfo(bo));
            case "DELETE" -> ResponseEntity.ok(studentContactApi.deleteStudentContactInfo(bo));
            default -> ResponseEntity.ok(Boolean.FALSE);
        };
    }

}
